package com.itheima.stock.service.impl;

import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserService;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVO;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @Author:gzc
 * @Date: 2025/10/23 20:42
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {


    private final IdWorkers idWorkers;
    private final Cache cache;

    private final SysUserMapper sysUserMapper;

    private final PasswordEncoder passwordEncoder;

    private static final int CAPTCHA_LENGTH = 4;

    public UserServiceImpl(IdWorkers idWorkers, Cache cache, SysUserMapper sysUserMapper, PasswordEncoder passwordEncoder) {
        this.idWorkers = idWorkers;
        this.cache = cache;
        this.sysUserMapper = sysUserMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public CaptchaVO generateCaptcha() {
        String captcha = RandomStringUtils.randomNumeric(CAPTCHA_LENGTH);
        String keySuffix = idWorkers.nextStrId();
        String redisKey = RedisKeyUtil.generateKey(Constants.CAPTCHA_PREFIX, keySuffix);
        cache.set(redisKey, captcha, Constants.CAPTCHA_EXPIRE);
        return CaptchaVO.builder()
                .code(captcha)
                .rkey(redisKey)
                .build();
    }

    @Override
    public LoginVO login(LoginDTO dto) {
        if (ObjectUtils.isEmpty(dto)
                || ObjectUtils.isEmpty(dto.getUsername())
                || ObjectUtils.isEmpty(dto.getPassword())
                || ObjectUtils.isEmpty(dto.getCode())
                || ObjectUtils.isEmpty(dto.getRkey())) {
            Exceptions.cast(CodeEnum.DATA_ERROR);
        }
        String captchaInRedis = cache.get(dto.getRkey());
        boolean equals = StringUtils.equals(captchaInRedis, dto.getCode());
        // 不相等
        if (!equals) {
            // 提示验证码错误
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        // 2.4 根据用户名查询用户信息
        SysUser user = sysUserMapper.selectByUsername(dto.getUsername(), Constants.NORMAL_USER, Constants.IS_NOT_DELETED);
        // 2.5 判断用户是否存在
        // 用户不存在
        if (ObjectUtils.isEmpty(user) ||
                !passwordEncoder.matches(dto.getPassword(), user.getPassword())) {
            // 提示用户账号或者密码错误
            Exceptions.cast(CodeEnum.SYSTEM_PASSWORD_ERROR);
        }
        // 3. 组装数据返回
        cache.remove(dto.getRkey());
        return LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .phone(user.getPhone())
                .nickName(user.getNickName())
                .build();
    }
}

